<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="归属仓库" prop="warehouseCode">
        <warehouse-select :warehouseCode="queryParams.warehouseCode"
          @submit="warehouseSelect($event, 'queryParams')"></warehouse-select>
      </el-form-item>
      <el-form-item label="库区名称" prop="regionName">
        <region-select :regionCode="queryParams.regionCode" :warehouseCode="queryParams.warehouseCode"
          @submit="regionSelect($event, 'queryParams')"></region-select>
      </el-form-item>
      <el-form-item label="货架代码" prop="shelfCode">
        <el-input v-model="queryParams.shelfCode" placeholder="请输入货架代码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="货架号" prop="shelfName">
        <el-input v-model="queryParams.shelfName" placeholder="请输入货架号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最大温度" prop="temperatureMax">
        <el-input v-model="queryParams.temperatureMax" placeholder="请输入最大温度" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最小温度" prop="temperatureMin">
        <el-input v-model="queryParams.temperatureMin" placeholder="请输入最小温度" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最大湿度" prop="humidityMax">
        <el-input v-model="queryParams.humidityMax" placeholder="请输入最大湿度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最小湿度" prop="humidityMin">
        <el-input v-model="queryParams.humidityMin" placeholder="请输入最小湿度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="长度" prop="leng">
        <el-input v-model="queryParams.leng" placeholder="请输入长度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="宽度" prop="width">
        <el-input v-model="queryParams.width" placeholder="请输入宽度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="高度" prop="height">
        <el-input v-model="queryParams.height" placeholder="请输入高度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="容积" prop="volume">
        <el-input v-model="queryParams.volume" placeholder="请输入容积" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="利用率" prop="availability">
        <el-input v-model="queryParams.availability" placeholder="请输入利用率" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最大承重" prop="weightMax">
        <el-input v-model="queryParams.weightMax" placeholder="请输入最大承重" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="通道" prop="thoroughfare">
        <el-input v-model="queryParams.thoroughfare" placeholder="请输入通道" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="层" prop="storey">
        <el-input v-model="queryParams.storey" placeholder="请输入层" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="格" prop="grid">
        <el-input v-model="queryParams.grid" placeholder="请输入格" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option v-for="dict in dict.type.sys_shelf_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="存放类型" prop="depositType">
        <el-select v-model="queryParams.depositType" placeholder="请选择存放类型" clearable>
          <el-option v-for="dict in dict.type.sys_shelf_deposit_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="u-m-b-10 u-m-t-10">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['wms:shelf:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['wms:shelf:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wms:shelf:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wms:shelf:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['wms:shelf:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="ckg2" plain icon="el-icon-delete" size="mini" @click="cleadRedis"
          v-hasPermi="['wms:shelf:cleadRedis']">清空缓存</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="ckg2" plain icon="el-icon-printer" size="mini" @click="print"
          v-hasPermi="['wms:shelf:print']">货架打印</el-button>
      </el-col>
      <el-col :span="1.5"><el-input v-model="width" placeholder="纸宽度mm" size="mini" style="width: 80px;" /></el-col>
      <el-col :span="1.5"><el-input v-model="height" placeholder="纸高度mm" size="mini" style="width: 80px;" /></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shelfList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="归属的仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="库区名称" align="center" prop="regionName" />
      <el-table-column label="货架代码" align="center" prop="shelfCode" />
      <el-table-column label="货架号" align="center" prop="shelfName" />
      <el-table-column label="最大温度" align="center" prop="temperatureMax" />
      <el-table-column label="最小温度" align="center" prop="temperatureMin" />
      <el-table-column label="最大湿度" align="center" prop="humidityMax" />
      <el-table-column label="最小湿度" align="center" prop="humidityMin" />
      <el-table-column label="长度" align="center" prop="leng" />
      <el-table-column label="宽度" align="center" prop="width" />
      <el-table-column label="高度" align="center" prop="height" />
      <el-table-column label="容积" align="center" prop="volume" />
      <el-table-column label="利用率" align="center" prop="availability" />
      <el-table-column label="最大承重" align="center" prop="weightMax" />
      <el-table-column label="通道" align="center" prop="thoroughfare" />
      <el-table-column label="层" align="center" prop="storey" />
      <el-table-column label="格" align="center" prop="grid" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_shelf_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="存放类型" align="center" prop="depositType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_shelf_deposit_type" :value="scope.row.depositType" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:shelf:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['wms:shelf:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改货架管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="库区名称" prop="regionName">
          <el-row :gutter="20">
            <el-col :span="11">
              <warehouse-select :warehouseCode="form.warehouseCode" @submit="warehouseSelect($event, 'form')"
                :appendToBody="false"></warehouse-select>
            </el-col>
            <el-col :span="11">
              <region-select :regionCode="form.regionCode" :warehouseCode="form.warehouseCode"
                @submit="regionSelect($event, 'form')"></region-select>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="货架代码" prop="shelfCode">
          <el-input v-model="form.shelfCode" placeholder="L-001-001" />
        </el-form-item>
        <el-form-item label="货架号" prop="shelfName">
          <el-input v-model="form.shelfName" placeholder="左一第一排" />
        </el-form-item>
        <el-form-item label="最大温度" prop="temperatureMax">
          <el-input v-model="form.temperatureMax" placeholder="请输入最大温度" type="Number" />
        </el-form-item>
        <el-form-item label="最小温度" prop="temperatureMin">
          <el-input v-model="form.temperatureMin" placeholder="请输入最小温度" type="Number" />
        </el-form-item>
        <el-form-item label="最大湿度" prop="humidityMax">
          <el-input v-model="form.humidityMax" placeholder="请输入最大湿度" type="Number" />
        </el-form-item>
        <el-form-item label="最小湿度" prop="humidityMin">
          <el-input v-model="form.humidityMin" placeholder="请输入最小湿度" type="Number" />
        </el-form-item>
        <el-form-item label="长度" prop="leng">
          <el-input v-model="form.leng" placeholder="请输入长度" type="Number" />
        </el-form-item>
        <el-form-item label="宽度" prop="width">
          <el-input v-model="form.width" placeholder="请输入宽度" type="Number" />
        </el-form-item>
        <el-form-item label="高度" prop="height">
          <el-input v-model="form.height" placeholder="请输入高度" type="Number" />
        </el-form-item>
        <el-form-item label="容积" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入容积" type="Number" />
        </el-form-item>
        <el-form-item label="利用率" prop="availability">
          <el-input v-model="form.availability" placeholder="请输入利用率" type="Number" />
        </el-form-item>
        <el-form-item label="最大承重" prop="weightMax">
          <el-input v-model="form.weightMax" placeholder="请输入最大承重" type="Number" />
        </el-form-item>
        <el-form-item label="通道" prop="thoroughfare">
          <el-input v-model="form.thoroughfare" placeholder="第一通道" />
        </el-form-item>
        <el-form-item label="层" prop="storey">
          <el-input v-model="form.storey" placeholder="第二层" />
        </el-form-item>
        <el-form-item label="格" prop="grid">
          <el-input v-model="form.grid" placeholder="第五格子" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option v-for="dict in dict.type.sys_shelf_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="存放类型" prop="depositType">
          <el-select v-model="form.depositType" placeholder="请选择存放类型">
            <el-option v-for="dict in dict.type.sys_shelf_deposit_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listShelf, getShelf, delShelf, addShelf, updateShelf, cleadRedis } from "@/api/wms/shelf";
import warehouseSelect from "../warehouse/components/warehouseSelect.vue";
import regionSelect from "../region/components/regionSelect.vue";
import { getToken } from "@/utils/auth";
import { getLodop } from '../../../plugins/LodopFuncs'
export default {
  name: "Shelf",
  dicts: ['sys_shelf_type', 'sys_shelf_deposit_type'],
  components: { warehouseSelect, regionSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货架管理表格数据
      shelfList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "id",
        isAsc: "desc",
        warehouseName: null,
        warehouseCode: null,
        regionName: null,
        regionCode: null,
        shelfCode: null,
        shelfName: null,
        temperatureMax: null,
        temperatureMin: null,
        humidityMax: null,
        humidityMin: null,
        leng: null,
        width: null,
        height: null,
        volume: null,
        availability: null,
        weightMax: null,
        thoroughfare: null,
        storey: null,
        grid: null,
        type: null,
        depositType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseName: [
          { required: true, message: "归属的仓库名称不能为空", trigger: "blur" }
        ],
        warehouseCode: [
          { required: true, message: "归属仓库的代码不能为空", trigger: "blur" }
        ],
        regionName: [
          { required: true, message: "库区名称不能为空", trigger: "blur" }
        ],
        regionCode: [
          { required: true, message: "库区代码不能为空", trigger: "blur" }
        ],
        shelfCode: [
          { required: true, message: "货架代码不能为空", trigger: "blur" }
        ],
        shelfName: [
          { required: true, message: "货架号不能为空", trigger: "blur" }
        ],
      },
      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/wms/shelf/importData"
      },
      shelfCodeList: [], // 选中货架后的货架条码列表
      width: '70mm', // 打印纸张宽度 单位毫米
      height: '40mm', // 打印纸张高度 单位毫米
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 货架打印 */
    print() {
      if (this.shelfCodeList.length == 0) {
        return this.$modal.msgError("请选择货架");
      }
      var LODOP2 = getLodop()
      LODOP2.SET_LICENSES('', '3B48D7BCFB760B72CAE6BC213A545D6F878', '', '')
      LODOP2.PRINT_INIT('货架打印')
      LODOP2.SET_PRINT_PAGESIZE(1, this.width, this.height, 2) // 设置40*70纸张大小,纸张小,改为横向打印
      for (const code of this.shelfCodeList) {
        LODOP2.NewPage();
        // LODOP2.SET_PRINT_PAGESIZE(3, 700, 400, 2) // 设置40*70纸张大小,纸张小,改为横向打印
        LODOP2.ADD_PRINT_BARCODE(30, 18, 242, 53, '128Auto', code)
      }
      // LODOP2.PRINT() // 打印
      LODOP2.PREVIEW() // 预览
    },
    /** 库位被选择*/
    regionSelect(row, form) {
      console.log("regionSelect:", row, form);
      if (row === undefined) {
        this[form].regionCode = null;
        this[form].regionName = null;
      } else {
        this[form].regionCode = row.regionCode;
        this[form].regionName = row.regionName;
      }
    },
    /** 仓库被选择*/
    warehouseSelect(row, form) {
      console.log("wartehouseSelect:", row, form);
      if (row === undefined) {
        this[form].warehouseCode = null;
        this[form].warehouseName = null;
        this[form].regionCode = null;
        this[form].regionName = null;
      } else {
        this[form].warehouseCode = row.warehouseCode;
        this[form].warehouseName = row.warehouseName;
      }
    },
    /** 查询货架管理列表 */
    getList() {
      this.loading = true;
      listShelf(this.queryParams).then(response => {
        this.shelfList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehouseName: null,
        warehouseCode: null,
        regionName: null,
        regionCode: null,
        shelfCode: null,
        shelfName: null,
        temperatureMax: null,
        temperatureMin: null,
        humidityMax: null,
        humidityMin: null,
        leng: null,
        width: null,
        height: null,
        volume: null,
        availability: null,
        weightMax: null,
        thoroughfare: null,
        storey: null,
        grid: null,
        type: null,
        depositType: null,
        createBody: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.shelfCodeList = selection.map((item) => item.shelfCode);
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加货架管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShelf(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货架管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShelf(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShelf(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除货架管理编号为"' + ids + '"的数据项？').then(function () {
        return delShelf(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/shelf/export', {
        ...this.queryParams
      }, `shelf_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('wms/shelf/importTemplate', {
      }, `template_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "货架管理导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 清空所有缓存 */
    cleadRedis() {
      cleadRedis().then(response => {
        this.$modal.msgSuccess("清空成功");
      });
    },
  }
};
</script>
<style scoped>
.el-form-item--small.el-form-item {
  margin-bottom: 1px !important;
}
</style>
